

#include "HeaderFiles.h"
#include "can_msg_hdl.h"

#define CAN_BAUDRATE  500

void can0_mailbox_init(void);
void can0_filter_init(void);


/*!
    \brief      can0 initialize configure
    \param[in]  none
    \param[out] none
    \retval     none
*/
void can0_init(void)
{
	
	rcu_periph_clock_enable(RCU_CAN0);

	gpio_init(GPIOA,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_11);      
	gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_12);    

	can0_mailbox_init();
	
	#if 0
	can0_filter_init();
	#endif
}


/*!
    \brief      can0 mailbox init configure
    \param[in]  none
    \param[out] none
    \retval     none
*/
void can0_mailbox_init(void)
{
	can_parameter_struct can_parameter;

	can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);

	can_deinit(CAN0);

	can_parameter.time_triggered        = DISABLE;    
    can_parameter.auto_bus_off_recovery = DISABLE;
    can_parameter.auto_wake_up          = DISABLE;
    can_parameter.no_auto_retrans       = DISABLE;
    can_parameter.rec_fifo_overwrite    = DISABLE;
    can_parameter.trans_fifo_order      = DISABLE;
    can_parameter.working_mode          = CAN_NORMAL_MODE;
    can_parameter.resync_jump_width     = CAN_BT_SJW_1TQ;
    can_parameter.time_segment_1        = CAN_BT_BS1_5TQ;
    can_parameter.time_segment_2        = CAN_BT_BS2_4TQ;

	/* 1MBps */
	#if CAN_BAUDRATE == 1000
	can_parameter.prescaler = 6;
	/* 500KBps */
	#elif CAN_BAUDRATE == 500
	can_parameter.prescaler = 12;
	/* 250KBps */
	#elif CAN_BAUDRATE == 250
	can_parameter.prescaler = 24;
	/* 125KBps */
	#elif CAN_BAUDRATE == 125
	can_parameter.prescaler = 48;
	/* 100KBps */
	#elif  CAN_BAUDRATE == 100
	can_parameter.prescaler = 60;
	/* 50KBps */
	#elif  CAN_BAUDRATE == 50
	can_parameter.prescaler = 120;
	/* 20KBps */
	#elif  CAN_BAUDRATE == 20
	can_parameter.prescaler = 300;
	#else
	#error "can baudrate erro"
	#endif  

    can_init(CAN0, &can_parameter);   
}



/*!
    \brief      can0 filter init configure
    \param[in]  none
    \param[out] none
    \retval     none
*/
void can0_filter_init(void)
{
	can_filter_parameter_struct can_filter;

    can_struct_para_init(CAN_INIT_STRUCT, &can_filter);

    // FIFO0 use to VCU
	can_filter.filter_number      = 0;	
    can_filter.filter_mode        = CAN_FILTERMODE_LIST;
    can_filter.filter_bits        = CAN_FILTERBITS_16BIT;
    can_filter.filter_list_high   = 0x00A1 << 5;
    can_filter.filter_list_low    = 0x00A2 << 5;
    can_filter.filter_mask_high   = 0x00A3 << 5;
    can_filter.filter_mask_low    = 0x0000 << 5;  
    can_filter.filter_fifo_number = CAN_FIFO1;
    can_filter.filter_enable      = ENABLE;
    can_filter_init(&can_filter);
    
    // FIF1 use to User debug
	can_filter.filter_number      = 3;	
    can_filter.filter_mode        = CAN_FILTERMODE_LIST;
    can_filter.filter_bits        = CAN_FILTERBITS_16BIT;
    can_filter.filter_list_high   = 0;
    can_filter.filter_list_low    = 0;
    can_filter.filter_mask_high   = 0x0000;
    can_filter.filter_mask_low    = 0x0000;  
    can_filter.filter_fifo_number = CAN_FIFO1;
    can_filter.filter_enable      = ENABLE;
    can_filter_init(&can_filter);
}








/*!
    \brief      can0 erro status reset
    \param[in]  none
    \param[out] none
    \retval     none
*/

void can_erro_reset(uint16_t runtime_ms)
{
	static uint16_t time_cnt = 0;
	
	if(can_error_get(CAN0) != CAN_ERROR_NONE)
	{		
		time_cnt += runtime_ms;
		if(time_cnt > CAN0_ERRO_TIME_THRE)
		{
			can0_init();        
			can_mailbox_init();  
			
			time_cnt = 0;
		}
	}
}












